package cn.hzu.controller;

import cn.hzu.bean.*;
import cn.hzu.mapper.UserMapper;
import cn.hzu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
public class userController {
    course course=new course();
    List<String> ch=new ArrayList<>();
    List<String> srcs=new ArrayList<>();
    @Autowired
    UserService userService;
    @Autowired
    private JavaMailSender javaMailSender;

    //获取发件人邮箱地址
    @Value("${spring.mail.username}")
    private String username;
    //发送HTML邮件
    @RequestMapping("/forgot")
    public String sendMail(HttpServletRequest request,
                           Map<String,Object> map) throws MessagingException {
        String email = request.getParameter("email");
        if(email==null) return "forgot";
        userService.updatestate(email);
        //发邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setFrom(username);
        helper.setTo(email);
        helper.setSubject("注册");
        String html = "<html>\n"+
                "<body>\n"+
                "<p>点击下方链接注册</p>\n"+
                "<a href=\"http://localhost:8080/reset\">http://localhost:8080/reset</a>"+
                "</body>\n"+
                "</html>";
        helper.setText(html,true);//邮箱内容
        javaMailSender.send(mimeMessage);//发送邮件
        map.put("msg","已向你的邮箱发送找回密码邮件，快去重置密码吧！");
        return "forgot";

    }

    //登陆界面
    @RequestMapping("/login")
    public String login(HttpServletRequest request,Map<String, Object> map){
        //登录前将所有用户状态重置为0
        userService.updateall();

        String email=request.getParameter("email");
        String psw=request.getParameter("password");

        User user=new User();
        user.setEmail(email);
        user.setPsw(psw);


        boolean flag = userService.login(user);
        if(flag) {
            userService.updatestate(user);
            return "/shouye";
        }
        else {
            map.put("msg","用户名密码错误或为空！");
            return "login";
        }

    }

    //首页
    @RequestMapping("/shouye")
    public String shouye(HttpServletRequest request){
        return "shouye";
    }
    //注册
    @RequestMapping("/register")
    public String register(HttpServletRequest request,
                           Map<String,Object> map){
        String email=request.getParameter("email");
        String psw=request.getParameter("password");
        User u=new User();
        u.setEmail(email);
        u.setPsw(psw);
        if(email==null) return "register";
        else{
            boolean flag=userService.register(u);
            if(flag) {
                map.put("msg","注册成功，请返回登陆！");
                return "register";
            }
            else{
                map.put("msg","该用户已存在！");
                return "register";
            }
        }
    }

    //密码重置
    @RequestMapping("/reset")
    public String reset(HttpServletRequest request,
                        Map<String,Object> map){
        String newpsw=request.getParameter("password");
        if(newpsw==null) return "reset";
        String email = userService.selectUser();//查找当前用户
        User user=new User();
        user.setPsw(newpsw);
        user.setEmail(email);
        userService.updatepsw(email,newpsw);
        map.put("msg","密码重置成功，请返回登陆！");
        return "reset";
    }
    //显示用户信息
    @RequestMapping("/contact")
    public String contact(Model model){
        User user = userService.findlogin();
        model.addAttribute("user",user);

        return "contact";
    }

    //修改用户信息
    @RequestMapping(value = "update_user",method = RequestMethod.POST)
    @ResponseBody
    public int update_user(String email, String name, String sex, String phone, String nickname){
        userService.upadteuser(email,name,sex,phone,nickname);
        return 1;
    }

    //课程浏览
    @RequestMapping("/index")
    public String index(Model model){
        User user=userService.findlogin();
        List<course> courses=userService.findAllc();
        System.out.println("canme_numb = "+courses.size());
        System.out.println("src = "+user.getImg_src());
        model.addAttribute("courses",courses);
        model.addAttribute("user",user);
        //srcs.clear();
        return "/index";
    }
    //我的课程
    @RequestMapping("/list")
    public String list(Model model){
        User user=userService.findlogin();
        List<course> courses=userService.findalluc(user.getId());
        model.addAttribute("courses",courses);
        model.addAttribute("user",user);
        return "list";
    }
    //我的笔记
    @RequestMapping("/show")
    public String show(Model model){
        User user=userService.findlogin();
        model.addAttribute("user",user);
        List<course_note> course_notes = userService.findnote(user.getEmail());
        model.addAttribute("notes",course_notes);
        return "show";
    }


    @RequestMapping("/watch_c")
    public String watch(Model model){
        return "watch_C";
    }

    //收藏课程
    @RequestMapping(value="add_ucourse",method=RequestMethod.POST)
    @ResponseBody
    public String add_ucourse(@RequestParam(value="name") String name) {
       User user = userService.findlogin();
      U_course u_course=new U_course(user.getId(),userService.find_id(name),name);
      if(userService.add_ucourse(u_course)){
       return "收藏成功："+name;}
      else
          return "已有该课程";
    }
    //取消课程收藏
    @RequestMapping(value="delete_ucourse",method=RequestMethod.POST)
    @ResponseBody
    public String delete_ucourse(@RequestParam(value="name") String name) {
        User user = userService.findlogin();
        U_course u_course=new U_course(user.getId(),userService.find_id(name),name);
        userService.delete_ucourse(u_course);
        return "取消收藏成功";
    }
    //删除课程笔记
    @RequestMapping(value = "delete_note", method = RequestMethod.POST)
    @ResponseBody
    public int delete_note(@RequestParam(value="id")int id){
        userService.delete_note(id);
        userService.update_id(id);
        return 1;
    }
    @RequestMapping("/ucourse")
    public String course(HttpServletRequest request,Model model,HttpServletResponse response) {
        String name= request.getParameter("name");
        String chapter=request.getParameter("chapter");
        int id=userService.find_id(name);
        List<String> h=userService.find_ch(id);
        course c=userService.findcourse(id);
        course.setId(c.getId());
        course.setImg_src(c.getImg_src());
        course.setName(c.getName());
        course.setIntro(c.getIntro());
        course.setTeacher(c.getTeacher());
        Course_src course_src=new Course_src(c.getId(),chapter," ");
        List<String> srcs1=userService.find(course_src);
        srcs.clear();
        srcs.addAll(srcs1);
        model.addAttribute("course",course);
        model.addAttribute("chapters",ch);
        ch.clear();
        ch.addAll(h);
        return "course";
    }
    @RequestMapping("/course")
    public String course(Model model){
        model.addAttribute("course",course);
        model.addAttribute("chapters",ch);
        return "course";
    }
    @RequestMapping("/ucontent")
    public String content(Model model, HttpServletRequest request){
        String chapter= request.getParameter("chapter");
        int id=course.getId();
        Course_src course_src=new Course_src(id,chapter," ");
        List<String> srcs1=userService.find(course_src);
        model.addAttribute("srcs",srcs1);
        srcs.clear();
        srcs.addAll(srcs1);
        return "content";
    }
    @RequestMapping("/content")
    public String content(Model model){
        model.addAttribute("srcs",srcs);
        return "content";
    }
    @RequestMapping("/write_note")
    public String write_note(HttpServletRequest request){
        String cname = request.getParameter("cname");
        String note = request.getParameter("note");
        System.out.println("课程名："+cname);
        //参数准备
        User user = userService.findlogin();
        String email=user.getEmail();
        int count=userService.count();

        course_note cnote=new course_note(count+1,email,cname,note);
        userService.add_note(cnote);
        return "content";
    }
}
